# -*- coding: utf-8 -*-
import scrapy
from scrapy.selector import Selector
from scrapy.http import Request
import re
from doubanMovie.items import DoubanmovieItem


class MoviestarSpider(scrapy.Spider):
    name = 'movieStar'
    allowed_domains = ['movie.douban.com']
    start_urls = []

    def start_requests(self):
        file_object = open('F:\CrawlMovie\star_number.txt', 'r')

        # update the flag of yn_award.txt
        file_award = open('F:\CrawlMovie\yn_award.txt', 'w+')
        try:
            file_award.write('0')
        finally:
            file_award.close()

        # read the number of each movie star.
        try:
            url_head = "https://movie.douban.com/celebrity/"
            for line in file_object:
                self.start_urls.append(re.sub(r'\n', '', (url_head + line)))
            for url in self.start_urls:
                yield self.make_requests_from_url(url)
        finally:
            file_object.close()

    def parse(self, response):
        selector = Selector(response)
        name = selector.xpath('//div[@id="content"]/h1/text()').extract()[0]
        constellation = selector.xpath('//div[@class="info"]/ul/li[2]/text()').extract()[1]
        occupation = selector.xpath('//div[@class="info"]/ul/li[5]/text()').extract()[1]
        awardsLink = selector.xpath('//div[@class="mod"]/div[@class="hd"]/h2/span/a/@href').extract()[2]

        # 去掉多余的符号或信息
        starName = re.sub(r' \w+', '', name)
        starOccupation = re.sub(r':', '', (re.sub(r'\s+', '', occupation)))
        starConstellation = re.sub(r':', '', (re.sub(r'\s+', '', constellation)))

        print(starName)
        print(starConstellation)
        print(starOccupation)

        # 五位演员基本信息存入star_info表中
        #item = DoubanmovieItem()

        #item['star_name'] = starName
        #item['star_occupation'] = starOccupation
        #item['star_constellation'] = starConstellation
        #yield item

        yield Request(awardsLink, callback=self.parse_award)

    # 获奖页面
    def parse_award(self, response):
        selector = Selector(response)
        awards = selector.xpath('//div[@class="article"]/div[@class="awards"]')
        tempName =selector.xpath('//div[@id="content"]/h1/text()').extract()[0]
        starName = re.sub(r' \w+', '', tempName)

        item = DoubanmovieItem()

        for award in awards:
           awardInYear = award.xpath('./div[@class="hd"]/h2/text()').extract()[0]
           yearAwards = award.xpath('./ul[@class="award"]')

           # 某年份获奖情况
           for yearAward in yearAwards:
               awardType = yearAward.xpath('./li[1]/a/text()').extract()[0]
               awardName = yearAward.xpath('./li[2]/text()').extract()[0]
               awardMovies = yearAward.xpath('./li[3]/a')
               temp = ''

               for awardMovie in awardMovies:
                   movieName = awardMovie.xpath('./text()').extract()[0]
                   temp += movieName + '/'
               temp2 = re.sub(r'/\Z', '', temp)
               #combineAward = awardType + ' ' + awardName + ' ' + temp2
               #print(combineAward)

               # 演员获奖情况存入star_awards表中
               #item['star_name'] = starName
               #item['award_year'] = awardInYear
               #item['award_type'] = awardType
               #item['award_name'] = awardName
               #if temp2:
                    #item['award_movie'] = temp2
               #else:
                    #item['award_movie'] = ''

               #yield item




